﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml

Public Class QLGiangVien
    Dim dtProvider As New GiangVienDAO()
    Private Sub QLGiangVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvDSGiangVien.DataSource = GiangVienDAO.GetData()
        'GiangVienDAO.GetData()
        LayDSGV()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangGiangVien As DataTable = GiangVienDAO.LayCauTrucBang()
            Dim gv As DataRow = bangGiangVien.NewRow()
            gv("MaGiangVien") = txtMaGV.Text.Trim()
            gv("HoTen") = txtHoTen.Text.Trim()
            gv("GioiTinh") = txtGioiTinh.Text.Trim()
            gv("NgaySinh") = txtNgaySinh.Value
            gv("SoDienThoai") = txtSoDT.Text.Trim()
            gv("CMND") = txtCMND.Text.Trim()
            gv("DiaChi") = txtDiaChi.Text.Trim()

            bangGiangVien.Rows.Add(gv)
            GiangVienDAO.CapNhat(bangGiangVien)
            LayDSGV()
            MessageBox.Show("Thêm thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        dgvDSGiangVien.Rows.Remove(dgvDSGiangVien.CurrentRow)
        Dim bangSinhVien As DataTable = dgvDSGiangVien.DataSource
        GiangVienDAO.CapNhat(bangSinhVien)
        LayDSGV()
        MessageBox.Show("Xóa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangGiangVien As DataTable = dgvDSGiangVien.DataSource
            Dim gv As DataRow = bangGiangVien.Rows(dgvDSGiangVien.CurrentRow.Index)
            gv.BeginEdit()
            gv("MaGiangVien") = txtMaGV.Text.Trim()
            gv("HoTen") = txtHoTen.Text.Trim()
            gv("GioiTinh") = txtGioiTinh.Text.Trim()
            gv("NgaySinh") = txtNgaySinh.Value
            gv("SoDienThoai") = txtSoDT.Text.Trim()
            gv("CMND") = txtCMND.Text.Trim()
            gv("DiaChi") = txtDiaChi.Text.Trim()
            gv.EndEdit()
            GiangVienDAO.CapNhat(bangGiangVien)
            LayDSGV()
            MessageBox.Show("Sửa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangGiangVien As DataTable = GiangVienDAO.LayCauTrucBang()
            Dim gv As DataRow = bangGiangVien.NewRow()
            gv("MaGiangVien") = txtMaGV.Text.Trim()
            gv("HoTen") = txtHoTen.Text.Trim()
            gv("GioiTinh") = txtGioiTinh.Text.Trim()
            gv("NgaySinh") = txtNgaySinh.Value
            gv("SoDienThoai") = txtSoDT.Text.Trim()
            gv("CMND") = txtCMND.Text.Trim()
            gv("DiaChi") = txtDiaChi.Text.Trim()

            bangGiangVien.Rows.Add(gv)
            GiangVienDAO.CapNhat(bangGiangVien)
            LayDSGV()
            MessageBox.Show("Update thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        If (MessageBox.Show("Bạn có chắc chắn muốn thoát hay không?", "Thoát khỏi chương trình", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            Me.Close()
            Global.System.Windows.Forms.Application.Exit()
        End If
    End Sub

    Private Sub dgvDSGiangVien_SelectionChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDSGiangVien.SelectionChanged
        Dim dong As DataGridViewRow = dgvDSGiangVien.CurrentRow
        If (dong Is Nothing) Then
            Exit Sub
        End If
        txtMaGV.Text = dong.Cells("MaGiangVien").Value.ToString()
        txtHoTen.Text = dong.Cells("HoTen").Value.ToString()
        txtGioiTinh.Text = dong.Cells("GioiTinh").Value.ToString()
        txtNgaySinh.Value = dong.Cells("NgaySinh").Value
        txtSoDT.Text = dong.Cells("SoDienThoai").Value.ToString()
        txtCMND.Text = dong.Cells("CMND").Value.ToString()
        txtDiaChi.Text = dong.Cells("DiaChi").Value.ToString()
    End Sub


    Public Function KiemTra() As String
        Dim chuoiLoi As String = ""
        If (String.Compare(txtMaGV.Text.Trim(), "", True) = 0) Then
            chuoiLoi &= "MaGiangVien phải khác rỗng" & vbCrLf
        End If

        If (String.Compare(txtHoTen.Text.Trim(), "", True) = 0) Then
            chuoiLoi &= "Họ tên phải khác rỗng" & vbCrLf
        End If
        Dim tuoi As Integer = Today.Year - txtNgaySinh.Value.Year
        If (tuoi < 18) Then
            chuoiLoi &= "Tuổi sinh viên không dưới 18" & vbCrLf
        End If
        Return chuoiLoi
    End Function

    Public Sub LayDSGV()
        'Dim bangGiangVien As DataTable = GiangVienDAO.LayDSGiangVien()
        'dgvDSGiangVien.DataSource = bangGiangVien
        dgvDSGiangVien.Columns(0).Visible = True
        dgvDSGiangVien.Columns(1).Visible = True
        dgvDSGiangVien.Columns(2).Visible = True
        dgvDSGiangVien.Columns(3).DefaultCellStyle.Format = "dd/MM/yyyy"
        dgvDSGiangVien.Columns(5).Visible = True

        dgvDSGiangVien.Columns(1).HeaderText = "Họ tên"
        dgvDSGiangVien.Columns(2).HeaderText = "Giới tính"
        dgvDSGiangVien.Columns(3).HeaderText = "Ngày sinh"
        dgvDSGiangVien.Columns(4).HeaderText = "Số điện thoại"
        dgvDSGiangVien.Columns(5).HeaderText = "CMND"
        dgvDSGiangVien.Columns(6).HeaderText = "DiaChi"

        For i As Integer = 0 To dgvDSGiangVien.Columns.Count - 1
            dgvDSGiangVien.Columns(i).ReadOnly = True
        Next
    End Sub
End Class